﻿using DevExpress.XtraEditors;
using gregn6Lib;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.Gestation
{
    public partial class GestationPrint : XtraForm
    {
        #region 变量声明
        private GridppReport Report = new GridppReport();
        NurFormsService.NursingForm formService = new NurFormsService.NursingForm();
        PatientInfo PatientInfo;
        Tb_User LoginUser;
        DateTime StatDate;
        DateTime EndDate;
        #endregion

        #region 构造函数
        public GestationPrint(Tb_User loginUser, PatientInfo patientInfo, DateTime StatDate, DateTime EndDate)
        {
            InitializeComponent();
            PatientInfo = patientInfo;
            LoginUser = loginUser;
            this.StatDate = StatDate;
            this.EndDate = EndDate;
            //载入报表模板数据
            Report.LoadFromFile(System.Windows.Forms.Application.StartupPath + @"\GRFModel\Ydhl_GRF\待产产程观察记录单.grf");
            //设置与数据源的连接串，因为在设计时指定的数据库路径是绝对路径。
            Report.ConnectionString = Utility.GetDatabaseConnectionString();
            //连接报表取数事件
            Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);
            //设定查询显示器关联的报表
            axGRPrintViewer1.Report = Report;
        }
        #endregion

        #region 连接报表获取事件
        public void ReportFetchRecord()
        {
            var ret = formService.GetNursingFormRecordByDate(this.PatientInfo.PATIENT_ID, this.PatientInfo.VISIT_ID, (int)FormType.Fetation, StatDate, EndDate);
            DataTable dt = new DataTable();
            if (ret.Code == 1)
            {
                CreateDataTableHead(dt);
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                list = list.OrderBy(p => p.AssessmentDate).ToList();
                for (int i = 0; i < list.Count; i++)
                {
                    NursingFormRecord record = list[i];
                    Pregnancy pcy = XmlUtil.XMLToObject<Pregnancy>(record.NursingFormData, Encoding.UTF8);
                    DataRow row = dt.NewRow();
                    row["PATIENT_NAME"] = PatientInfo.PATIENT_NAME;    //患者姓名
                    row["SEX"] = PatientInfo.SEX;      //性别
                    row["AGE"] = PatientInfo.AGE;      //年龄
                    row["NURSE_CELL_NAME"] = PatientInfo.NURSE_CELL_NAME;   //病区
                    row["DEPARTMENT_NAME"] = PatientInfo.DEPARTMENT_NAME;   //科室
                    row["BED_NO"] = PatientInfo.BED_NO;    //床号
                    row["PATIENT_ID"] = PatientInfo.PATIENT_ID;  //住院号
                    row["RiQi"] = pcy.RiQi;     //日期
                    row["ShiJian"] = DateTimeHelper.HHmmssToHHmm(pcy.ShiJian);   //时间
                    row["XueYa_Zuo"] = pcy.XueYa_Zuo + "/" + pcy.XueYa_You;    //血压_左
                    row["XueYa_You"] = pcy.XueYa_You;   //血压_右
                    row["TaiDongJiShu"] = pcy.TaiDongJiShu;    //胎动计数
                    row["TaiFangWei"] = pcy.TaiFangWei;    //胎方位
                    row["TaiXianLu"] = pcy.TaiXianLu;   //胎先露
                    row["TaiXinWeiZhi"] = pcy.TaiXinWeiZhi;    //胎心位置
                    row["TaiXinCiShu"] = pcy.TaiXinCiShu;   //胎心次数
                    row["GongSuoChiXu"] = pcy.GongSuoChiXu;   //宫缩持续
                    row["GongSuoJianXie"] = pcy.GongSuoJianXie;   //宫缩间歇
                    row["GongSuoQiangDu"] = pcy.GongSuoQiangDu;    //宫缩强度
                    row["GongKouWeiZhi"] = pcy.GongKouWeiZhi;   //宫口位置
                    row["GongKouZhiDi"] = pcy.GongKouZhiDi;    //宫口质地
                    row["GongKouDaXiao"] = pcy.GongKouDaXiao;  //宫口大小
                    row["XianLuGaoDi"] = pcy.XianLuGaoDi;    //先露高低
                    row["TaiMo"] = pcy.TaiMo;   //胎膜
                    row["CuiChanSuDianDiNongDu"] = pcy.CuiChanSuNongDu;   //催产素点滴_浓度
                    row["CuiChanSuDianDiDiShu"] = pcy.CuiChanSuDiShu;    //催产素点滴_滴数
                    row["QiTa"] = pcy.QiTa;         //其他
                    row["HuShiQiamMing"] = pcy.QianMing;     //签名
                    dt.Rows.Add(row);
                }
                dt.DefaultView.Sort = "RiQi ASC,ShiJian ASC";
                DataTable data = dt.DefaultView.ToTable();
                for (int i = 0; i < data.Rows.Count; i++)
                {
                    data.Rows[i]["RiQi"] = Convert.ToDateTime(data.Rows[i]["RiQi"]).ToString("MM-dd");
                }
                //将 DataTable 的数据转储到 Grid++Report 的数据集中
                Utility.FillRecordToReport(Report, data);
            }
            else if (ret.Code == 2)
            {
                CreateDataTableHead(dt);
                DataRow dr = dt.NewRow();
                dt.Rows.Add(dr);
                Utility.FillRecordToReport(Report, dt);
            }
            else if (ret.Code == 0)
            {
                MessageBox.Show(ret.M);
            }
        }

        private static void CreateDataTableHead(DataTable dt)
        {
            dt.Columns.Add("PATIENT_NAME", typeof(string));    //患者姓名
            dt.Columns.Add("SEX", typeof(string));      //性别
            dt.Columns.Add("AGE", typeof(string));      //年龄
            dt.Columns.Add("NURSE_CELL_NAME", typeof(string));   //病区
            dt.Columns.Add("DEPARTMENT_NAME", typeof(string));   //科室
            dt.Columns.Add("BED_NO", typeof(string));    //床号
            dt.Columns.Add("PATIENT_ID", typeof(string));   //住院号
            dt.Columns.Add("RiQi", typeof(string));      //日期
            dt.Columns.Add("ShiJian", typeof(string));   //时间
            dt.Columns.Add("XueYa_Zuo", typeof(string));    //血压_左
            dt.Columns.Add("XueYa_You", typeof(string));   //血压_右
            dt.Columns.Add("TaiDongJiShu", typeof(string));    //胎动计数
            dt.Columns.Add("TaiFangWei", typeof(string));    //胎方位
            dt.Columns.Add("TaiXianLu", typeof(string));   //胎先露
            dt.Columns.Add("TaiXinWeiZhi", typeof(string));    //胎心位置
            dt.Columns.Add("TaiXinCiShu", typeof(string));   //胎心次数
            dt.Columns.Add("GongSuoChiXu", typeof(string));    //宫缩持续
            dt.Columns.Add("GongSuoJianXie", typeof(string));   //宫缩间歇
            dt.Columns.Add("GongSuoQiangDu", typeof(string));    //宫缩强度
            dt.Columns.Add("GongKouWeiZhi", typeof(string));   //宫口位置
            dt.Columns.Add("GongKouZhiDi", typeof(string));    //宫口质地
            dt.Columns.Add("GongKouDaXiao", typeof(string));   //宫口大小
            dt.Columns.Add("XianLuGaoDi", typeof(string));    //先露高低
            dt.Columns.Add("TaiMo", typeof(string));   //胎膜
            dt.Columns.Add("CuiChanSuDianDiNongDu", typeof(string));   //催产素点滴_浓度
            dt.Columns.Add("CuiChanSuDianDiDiShu", typeof(string));    //催产素点滴_滴数
            dt.Columns.Add("QiTa", typeof(string));         //其他
            dt.Columns.Add("HuShiQiamMing", typeof(string));     //签名

        }
        #endregion

        #region 窗体加载启动锐浪报表
        private void Form1_Load(object sender, EventArgs e)
        {
            axGRPrintViewer1.Start();
        }
        #endregion
    }
}